import com.sun.deploy.net.offline.WIExplorerOfflineHandler;

import java.util.Comparator;

public class Test33 {
    /**
     * 自定义比较器
     */
    public static class  MyComparator implements Comparator<String> {

        @Override
        public int compare(String o1, String o2) {
            if (o1 == null || o2 == null) throw new IllegalArgumentException("invalid");
            String s1 = o1+o2;
            String s2 = o2+o1;
            return s1.compareTo(s2);
        }
    }

    /**
     * 快速排序
     * @param array
     * @param start
     * @param end
     * @param comparator
     */
    public static void quicksort(String[] array,int start,int end,Comparator<String> comparator){
        if (start>= end) return;
        String pivot = array[start];//基准书
        int left = start;
        int right = end;
        while(left < right){
            while (left < right && comparator.compare(array[right],pivot)>=0){
                right --;
            }
            while (left < right && comparator.compare(array[left],pivot) <= 0){
                left++;
            }
            if (left < right){
                String temp = array[left];
                array[left] = array[right];
                array[right] = temp;
            }

        }
        //讲基准数归位
        array[start] = array[left];
        array[left] = pivot;
        quicksort(array,start,left-1,comparator);
        quicksort(array,left+1,end,comparator);
    }

    public static String printMinNumber(String[] array){
        if (array == null ||array.length ==0) throw new IllegalArgumentException("invalid");
        MyComparator myComparator = new MyComparator();
        quicksort(array,0,array.length-1,myComparator);
        StringBuilder builder = new StringBuilder();//不是线程安全的，效率高一些
        for (String s : array){
            builder.append(s);
        }
        return builder.toString();
    }


    public static void main(String[] args) {
        String[] data = {"3", "5", "1", "4", "2"};
        System.out.println(printMinNumber(data));
    }

}
